package cn.yikaojiuguo.student.ui.usercenter;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.yikaojiuguo.student.R;
import cn.yikaojiuguo.student.app.YiKaoApplication;
import cn.yikaojiuguo.student.support.simple.SimpleTextWatcher;
import cn.yikaojiuguo.student.support.simple.UrlConstants;
import cn.yikaojiuguo.student.support.utils.LogUtil;
import cn.yikaojiuguo.student.support.utils.RequestUtil;
import cn.yikaojiuguo.student.support.utils.ToastUtil;
import cn.yikaojiuguo.student.support.utils.Util;
import cn.yikaojiuguo.student.support.volley.Response;
import cn.yikaojiuguo.student.ui.base.BaseLoginActivity;
import cn.yikaojiuguo.student.ui.global.SocialLoginPopupWindow;

public class LoginActivity extends BaseLoginActivity {

    @Bind(R.id.rl_root)
    RelativeLayout mRl_root;
    @Bind(R.id.et_number)
    EditText mEt_number;
    @Bind(R.id.iv_next)
    ImageView mIv_next;
    @Bind(R.id.ll_number)
    LinearLayout mLl_number;

    private SocialLoginPopupWindow mPopupWindow;

    // 整个平台的Controller, 负责管理整个SDK的配置、操作等处理
    private UMSocialService mController;

    public static Intent newIntent() {
        Intent intent = new Intent(YiKaoApplication.getInstance(),LoginActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mController = UMServiceFactory.getUMSocialService("com.umeng.login");
        ButterKnife.bind(this);
        initPopupWindow();
        initEvents();
    }

    private void initPopupWindow() {
        mPopupWindow = new SocialLoginPopupWindow(this,0.95f,0.2f);
        mPopupWindow.setAnimationStyle(R.style.anim_popup_from_bottom);
    }

    private void initEvents() {
        mIv_next.setOnClickListener(LoginActivity.this);
        mEt_number.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() == 11) {
                    mIv_next.setVisibility(View.VISIBLE);
                    mEt_number.setTextColor(getResources().getColor(R.color.text_white));
                    mLl_number.setBackgroundResource(R.drawable.shape_background_longin_number_finish);
                } else {
                    mIv_next.setVisibility(View.GONE);
                    mEt_number.setTextColor(getResources().getColor(R.color.text_black));
                    mLl_number.setBackgroundResource(R.drawable.shape_background_longin_number);
                }
            }
        });
        // 键盘改为-->
        mEt_number.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_GO) {
                    next();
                }
                return true;
            }
        });
        Util.openSoftInput(mEt_number);
    }

    @Override
    protected String getOperationText() {
        return getResources().getString(R.string.activity_login_operation_text_more);
    }

    private void next() {
        final String number = mEt_number.getText().toString().trim();
        if(Util.isMoblie(number)) {
            showProgressDialog();
            final String requestUrl = UrlConstants.CHECK_MOBLIE + "&mobile=" + number;
            RequestUtil.requestGet(requestUrl, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    dismissProgressDialog();
                    if (response != null) {
                        if (response.optBoolean("success")) {
                            //通过返回的ecode判断状态
                            //-1:请求过于频繁
                            //1:发送成功
                            //2:已经注册
                            int ecode = response.optJSONObject("data").optInt("ecode");
                            LogUtil.showInfoLog("ecode", "ecode = " + ecode);
                            String message = "";
                            if(ecode == -1) {//同一号码请求过于频繁
                                message = response.optJSONObject("data").optString("message");
                                if(!TextUtils.isEmpty(message)) {
                                    ToastUtil.showToast(LoginActivity.this,message);
                                }
                            } else if (ecode == 1) {//验证码发送成功，前往输入验证码
                                //message为验证码
                                message = response.optJSONObject("data").optString("message");
                                YiKaoApplication.putMobileNumber(number);
                                YiKaoApplication.putCaptcha(message);
                                startActivity(CaptchaInputActivity.newIntent(number,message));
                                finish();
                                rightEnter();
                            } else if(ecode == 2) {//已经注册，前往输入密码
                                //手机号码保存到本地
                                YiKaoApplication.putMobileNumber(number);
                                startActivity(PassWordInputActivity.newIntent());
                                finish();
                                rightEnter();
                            }
                        }
                    }
                }
            }, null, mTag);
        } else {
            ToastUtil.showToast(this, "请输入正确的手机号码！");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_operation:
                Util.closeSoftInput(this);
                mPopupWindow.showAtLocation(mRl_root, Gravity.BOTTOM, 0, 0);
                break;
            case R.id.iv_next:
                next();
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
